package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Aboutus;
import com.wn.class30.entity.JoinImgCase;
import com.wn.class30.entity.JoinImgHead;
import com.wn.class30.entity.JoinImgMeal;
import com.wn.class30.service.AboutusService;
import com.wn.class30.service.JoinImgCaseService;
import com.wn.class30.service.JoinImgHeadService;
import com.wn.class30.service.JoinImgMealService;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/houtai/page/deploy/join/")
@Slf4j
public class JoinIndexController {
    @Resource
    private JoinImgHeadService joinImgHeadService;

    @Resource
    private JoinImgMealService joinImgMealService;

    @Resource
    private JoinImgCaseService joinImgCaseService;

    @Resource
    private AboutusService aboutusService;

    @RequestMapping("getJoinImgHeads")
    public Result getJoinImgHeads()throws Exception{
        List<JoinImgHead> joinImgHeads = joinImgHeadService.selectJoinImgHeads();
        return new Result("200","success",null,joinImgHeads);
    }
    @RequestMapping("getJoinImgMeals")
    public Result getJoinImgMeals()throws Exception{
        List<JoinImgMeal> joinImgMeals = joinImgMealService.selectJoinImgMeals();
        return new Result("200","success",null,joinImgMeals);
    }
    @RequestMapping("getJoinImgCases")
    public Result getJoinImgCases()throws Exception{
        List<JoinImgCase> joinImgCases = joinImgCaseService.selectJoinImgCase();
        return new Result("200","success",null,joinImgCases);
    }
    @RequestMapping("getAboutus")
    public Result getAboutus()throws Exception{
        Aboutus aboutus = aboutusService.selectAboutusById();
        return new Result("200","success",aboutus,null);
    }

    @RequestMapping(value = "updateHead",method = RequestMethod.POST)
    public Result updateHead( @RequestBody List<JoinImgHead> imgHead)throws Exception{
        log.info("imgHead=============================="+imgHead);
        //删除原有图片
        joinImgHeadService.deleteImgHeads();
        for (JoinImgHead joinImgHead : imgHead) {
            joinImgHeadService.insertImgHead(joinImgHead);
        }
        return new Result("200","success",null,null);
    }

    @RequestMapping(value = "updateMeal",method = RequestMethod.POST)
    public Result updateMeal( @RequestBody List<JoinImgMeal> imgMeal)throws Exception{
        log.info("imgMeal=============================="+imgMeal);
        joinImgMealService.deleteImgMeals();
        for (JoinImgMeal joinImgMeal : imgMeal) {
            joinImgMealService.insertImgMeal(joinImgMeal);
        }
        return new Result("200","success",null,null);
    }

    @RequestMapping(value = "updateCase",method = RequestMethod.POST)
    public Result updateCase( @RequestBody List<JoinImgCase> imgCase)throws Exception{
        log.info("imgCase=============================="+imgCase);
        joinImgCaseService.deleteImgCases();
        for (JoinImgCase joinImgCase : imgCase) {
            joinImgCaseService.insertImgCase(joinImgCase);
        }
        return new Result("200","success",null,null);
    }

    @RequestMapping(value = "updateAboutus")
    public Result updateAboutus(String aboutus)throws Exception{
        log.info("aboutus=============================="+aboutus);
        if(aboutus != null && !"".equals(aboutus.trim())){
            Aboutus aboutus1 = aboutusService.selectAboutusById();
            aboutus1.setAboutusDesc(aboutus);
            aboutusService.updateAboutusById(aboutus1);
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }

}
